<?php
declare(strict_types=1);
namespace App\Dao;

use App\Model\{Wxvisit,Wxdata};

class WxvisitdDao
{
    /** 查看时间段内访问量
     * @Author abell
     * @DateTime 2020-05-27
     * @return int
     */
    public function webNum($start_time,$end_time,$type_str){
        return Wxvisit::query()->where(['type_explain'=>$type_str])
            ->whereBetween('add_time',[$start_time,$end_time])
            ->count();
    }

    /** 查看时间段内访问量
     * @Author abell
     * @DateTime 2020-05-27
     * @return int
     */
    public function visitNum($start_time,$end_time){
        return Wxvisit::query()->whereIn('p_id',[0,1])
            ->whereBetween('add_time',[$start_time,$end_time])
            ->count();
    }

    /** 查看访问页面路由列表
     * @Author abell
     * @DateTime 2020-05-27
     * @return array
     */
    public function webList(){
        return Wxvisit::query()->select('type_explain')
            ->whereBetween('p_id',[0,1])
            ->distinct()
            ->get();
    }

    /** 查看是否存在,不存在则写入数据
     * @Author abell
     * @DateTime 2020-05-27
     * @return array
     */
    public function weekData($start_time,$end_time){
        return Wxvisit::query()->where(['p_id'=>2])
            ->orderBy('add_time','ASC')
            ->whereBetween('add_time',[$start_time,$end_time])
            ->select('watch','add_time')
            ->get()->toArray();
    }

    /** 查看是否存在,不存在则写入数据
     * @Author abell
     * @DateTime 2020-05-27
     * @return int
     */
    public function visitHistory($right_time){
        $has_visit = Wxvisit::query()->where(['add_time'=>$right_time,'p_id'=>2])->exists();
        if(!$has_visit){
            $insert_visit = Wxvisit::query()->whereBetween('add_time',[$right_time,$right_time+86399])
                ->where(['p_id'=>0])->count();
            return Wxvisit::query()->insertGetId([
                'p_id' => 2,
                'add_time' => $right_time,
                'update_time' => $right_time+86399,
                'watch' => $insert_visit]);
        }
    }

    /** 查询数据
     * @Author abell
     * @DateTime 2020-05-27
     * @return object
     */
    public function getWxdata($where,$start_time,$end_time,$select=['*']){
        return Wxdata::query()
            ->where($where)
            ->whereBetween('add_time',[$start_time,$end_time])
            ->orderBy('add_time','desc')
            ->select($select)->get();
    }

    /** 留存数据获取处理
     * @Author abell
     * @DateTime 2020-05-27
     * @return object
     */
    public function dayRetain($start_time,$end_time,$pageSize,$pagenum=1){
        return Wxdata::query()->whereBetween('add_time',[$start_time,$end_time])
            ->orderBy('add_time','desc')
            ->select('daily_retain','add_time')
            ->paginate(intval($pageSize),['*'],'page',intval($pagenum));
    }

    /** 查询特定排序的一个统计数据
     * @Author abell
     * @DateTime 2020-05-27
     * @return object
     */
    public function searchData($order,$rule='desc'){
        return Wxdata::query()->orderBy($order,$rule)->first();
    }

    /** 查询时间段内微信记录数量
     * @Author abell
     * @DateTime 2020-05-27
     * @return int
     */
    public function sumData($key,$start_time,$end_time,$target){
        return Wxdata::query()->whereBetween($key,[$start_time,$end_time])->sum($target);
    }

    /** 查询数据数量
     * @Author abell
     * @DateTime 2020-05-27
     * @return int
     */
    public function countData($where,$option=false){
        if($option){
            return Wxvisit::query()->where($where)->where($option['key'],$option['symbol'],$option['value'])->count();
        }else{
            return Wxvisit::query()->where($where)->count();
        }

    }
}
